var evapic = ws = order_id = member_id = rec_id = positions = spage = zt = '';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	ws = plus.webview.currentWebview();
	order_id = ws.order_id;
	spage = ws.spage;
	zt = ws.zt;
	//处理
	$.ajax({
		type:"get",
		url:apiRoot + "?act=member_evaluate&op=index",
		data:{
			order_id:order_id,
			member_id:member_id
		}, 
		dataType:'json',
		success:function(data){
//			console.log(JSON.stringify(data));
			if(data.code == 200){
				var html = ''; 
				$.each(data.datas.order_goods, function(k,v) {
					html += '<div class="edit-addr">'+
								'<ul class="mui-table-view">'+
									'<li class="mui-table-view-cell mui-media">'+
										'<img class="mui-media-object mui-pull-left" src="'+ pics + v.store_id + '/' + v.goods_image +'">'+
										'<div class="mui-media-body">'+
											'<p class="mui-ellipsis">'+ v.goods_name +'</p>'+
											'<span style="color: red;">￥'+ v.goods_pay_price +'</span>'+
										'</div>'+	 
									'</li>'+	 
								'</ul>'+	 
					     	 	'<textarea name="" rows="6" cols="" class="comment" placeholder="请输入你的评价(限字150)"></textarea>'+
								'<div class="bbs-pulish-img rec_id count'+ k +'" data-count="1" data-rec_id="'+ v.rec_id +'">'+
									'<a class="Ico mui-icon">'+
										'<i class="iconfont mobilepic" data-type="count'+ k +'">&#xe697;</i>'+
									'</a>'+
								'</div>'+	
							'</div>';
				}); 
				$('.mui-content').html(html);
				
				$('.mobilepic').on('click',function(){
					positions = $(this).attr('data-type');
					plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
						if(e.index == 1){
							var car =plus.camera.getCamera();
							car.captureImage(function(path){
								//展示图片
			//					alert(plus.io.convertLocalFileSystemURL(path));
			//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
								appendpic(path);
								
							},function(err){});
							
						}else if(e.index == 2){
							plus.gallery.pick(function(path){
			//					$('.shangchuan').attr('src',path);
								appendpic(path);
							});
						}
					});
				})
				
				
				$('.fabiao').on('click',function(){
					var evaluate_images = rec_ids = comments = '';
					$('.rec_id').each(function(){
						var _this = $(this);
						rec_ids += _this.attr('data-rec_id')+',';
//						console.log(rec_ids);
						var evapic = '';
						$(this).find('img').each(function(){
							var str = $(this).attr('src');
							var index = str.lastIndexOf("\/");  
							evapic += str.substring(index + 1, str.length) + ',';
						});
						evaluate_images += '|' + evapic;
					});
					$('.comment').each(function(){
						comments += $(this).val() + ',';
					});
//					console.log(evaluate_images+'---'+rec_ids+'----'+comments);
					$.ajax({
						type:"post",
						url:apiRoot + "?act=member_evaluate&op=save",
						data:{
							member_id:member_id,
							order_id:order_id,
							evaluate_images:evaluate_images,
							rec_ids:rec_ids,
							comments:comments
						},
						dataType:'json',
						success:function(data){
							console.log(JSON.stringify(data));
							if(data.code == 200){spage
								toast('评价成功！');
									plus.webview.getWebviewById(spage).reload();
									if(zt == 2){
										plus.webview.getWebviewById('order.html').reload();
									}
									setTimeout(function(){
										ws.close();
									},30)
							}else{
								toast(data.datas.error);
							}
						},
						error:function(e){
							console.log(JSON.stringify(e));
							error();
						}
					});
				})
			}
		},
		error:function(e){
			console.log(JSON.stringify(e));
		}
	});
	
	
	
	
	
	
})
//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
	var counts = $('.'+positions).attr('data-count');
//	console.log(positions+'--'+counts);
	if(counts <= 5){
		if(files.length <= 0){
			toast("请上传头像！");
		}
//				console.log(JSON.stringify(files.path));return;
		var server = apiRoot + "?act=sns_album&op=file_uploadone&member_id="+member_id;  
		var task = plus.uploader.createUpload(server,
		    {method:"post"},
			function(t,status){ //上传完成
				if(status == 200){ 
					plus.nativeUI.closeWaiting();
//					console.log(123);
					var infopics = $.parseJSON(t.responseText);
//					console.log(JSON.stringify(infopics));
					if(infopics.code == 200){
						$('.'+positions).attr('data-count',Number(counts) + 1);
						$('.'+positions).prepend('<img class="evapic" src="'+ infopics.datas.file_url +'" data-preview-src="" data-preview-group="1"/>');
					}else{
						toast(infopics.datas.error);
					}
	//					plus.nativeUI.toast('处理完成' + t.responseText);
				}else{
					toast('上传失败：' + status);
					plus.nativeUI.closeWaiting();
				}
				
			},function () {
				plus.nativeUI.closeWaiting();
			}
			
		); 
		for(var i = 0;i　<　files.length ; i++){
			var f =files[i];
			task.addFile(f.path,{key:'file'});
	//					    console.log(f.name)
		}
		task.start();
	}else{
		toast('最多上传五张图片');
		plus.nativeUI.closeWaiting();
	} 
	 
}		
